/*************************************************************************
    > File Name: hlib.h
    > Author: 侯广臣
    > Mail: 2801059691@qq.com 
    > Created Time: 2020年11月29日 星期日 13时18分09秒
 ************************************************************************/
#ifndef _HLIB_H_
#define _HLIB_H_

#include <iostream>
#include <deque>
#include <algorithm>

#include <ctime>
#include <cstdlib>

class CRand
{
	public:
		//产生cont个包含begin到end的随机数，mod = true不重复，mod = false 重复
		//如果begin > end 或者 cont == 0 产生0个随机数
		//如果cont > end -begin + 1 并且 mod == true ，产生 0 个随机数
		CRand(unsigned int begin = 0, unsigned int end = 0, unsigned int cont = 0, bool mod = true);
		~CRand();

		//向已经存在的随机数里插入cont个包含begin到end的随机数，mod = true不重复，mod = false 重复
		void insert(unsigned int begin, unsigned int end, unsigned int cont, bool mod = true);

		std::deque<unsigned int>::iterator begin();
		std::deque<unsigned int>::iterator end();
		void pop_back();
		void pop_front();
		unsigned int front();
		unsigned int back();
		size_t size();
		bool empty();
		void clear();
		


	private:
		std::deque<unsigned int> *m_d;

};


//产生cont个包含begin到end的随机数，mod = true不重复，mod = false 重复
//如果begin > end 或者 cont == 0 产生0个随机数
//如果cont > end -begin + 1 并且 mod == true ，产生0个随机数
CRand::CRand(unsigned int begin, unsigned int end , unsigned int cont, bool mod)
{
	m_d = new std::deque<unsigned int>;
	insert(begin, end, cont, mod);
}

CRand::~CRand()
{	
	delete m_d;
}

//向已经存在的随机数里插入cont个包含begin到end的随机数，mod = true不重复，mod = false 重复
void CRand::insert(unsigned int begin, unsigned int end, unsigned int cont, bool mod)
{
	if(cont == 0 || begin > end || cont > end-begin+1){
		return;
	}

	srand(time(0));
	unsigned int tmp;
	while(cont != 0){
		tmp = rand() % (end - begin + 1) + begin;
		if(mod){
			if(std::find(m_d->begin(), m_d->end(), tmp) == m_d->end()){
				m_d->push_back(tmp);
				cont--;
			}
		}
		else{
			m_d->push_back(tmp);
			cont--;
		}
	}
	return;
}


//  以下函数均调用deque成员函数实现 

std::deque<unsigned int>::iterator CRand::begin()
{
	return m_d->begin();
}

std::deque<unsigned int>::iterator CRand::end()
{
	return m_d->end();
}

void CRand::pop_back()
{
	m_d->pop_back();
}

void CRand::pop_front()
{
	m_d->pop_front();
}

unsigned int CRand::front()
{
	return m_d->front();
}

unsigned int CRand::back()
{
	return m_d->back();
}

size_t CRand::size()
{
	return m_d->size();
}

bool CRand::empty()
{
	return m_d->empty();
}

void CRand::clear()
{
	m_d->clear();
}


#endif
